/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.alien.admin.modules.publisher.rest;

import com.alien.admin.annotation.Log;
import com.alien.admin.utils.enums.FlowNodeIdEnum;
import com.alien.admin.modules.publisher.domain.PublishSheetApprovalRecord;
import com.alien.admin.modules.publisher.service.PublishSheetApprovalRecordService;
import com.alien.admin.modules.publisher.service.dto.PublishSheetApprovalRecordDto;
import com.alien.admin.modules.publisher.service.dto.PublishSheetApprovalRecordQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author zengzhifang
* @date 2021-10-25
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "发布审核记录管理")
@RequestMapping("/api/publishSheetApprovalRecord")
public class PublishSheetApprovalRecordController {

    private final PublishSheetApprovalRecordService publishSheetApprovalRecordService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('publishSheetApprovalRecord:list')")
    public void download(HttpServletResponse response, PublishSheetApprovalRecordQueryCriteria criteria) throws IOException {
        publishSheetApprovalRecordService.download(publishSheetApprovalRecordService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询发布审核记录")
    @ApiOperation("查询发布审核记录")
    @PreAuthorize("@el.check('publishSheetApprovalRecord:list')")
    public ResponseEntity<Object> query(PublishSheetApprovalRecordQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(publishSheetApprovalRecordService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    /**
     * 查询某个单据的所有审批记录
     *
     * @param publishSheetId
     * @return
     */
    @GetMapping(value = "findByPublishSheetId")
    @Log("查询某个单据的所有审批记录")
    @ApiOperation("查询发布审核记录")
    @PreAuthorize("@el.check('publishSheetApprovalRecord:list')")
    public ResponseEntity<Object> findByPublishSheetId(Long publishSheetId){
        PublishSheetApprovalRecordQueryCriteria criteria = new PublishSheetApprovalRecordQueryCriteria();
        criteria.setPublishSheetFlowId(publishSheetId);
        List<PublishSheetApprovalRecordDto> dtoList =  publishSheetApprovalRecordService.queryAll(criteria);
        for(PublishSheetApprovalRecordDto dto : dtoList){
            dto.setFlowNodeName(FlowNodeIdEnum.getById(dto.getFlowNodeId()).getName());
        }
        return new ResponseEntity<>(dtoList, HttpStatus.OK);
    }

    @PostMapping
    @Log("新增发布审核记录")
    @ApiOperation("新增发布审核记录")
    @PreAuthorize("@el.check('publishSheetApprovalRecord:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody PublishSheetApprovalRecord resources){
        resources.setCreateTime(new Timestamp(System.currentTimeMillis()));
        return new ResponseEntity<>(publishSheetApprovalRecordService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改发布审核记录")
    @ApiOperation("修改发布审核记录")
    @PreAuthorize("@el.check('publishSheetApprovalRecord:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody PublishSheetApprovalRecord resources){
        publishSheetApprovalRecordService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除发布审核记录")
    @ApiOperation("删除发布审核记录")
    @PreAuthorize("@el.check('publishSheetApprovalRecord:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        publishSheetApprovalRecordService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
